<template>
  <div v-for="(item, index) in chartData" :key="index">
    <div class="chart-item">
      <div class="title">
        {{ item.name }}
        <div class="tools"></div>
      </div>
      <table style="width: 100%">
        <thead>
          <tr>
            <th :colspan="4">{{ item.name }}</th>
          </tr>
          <tr>
            <th rowspan="1" colspan="1">项</th>
            <th rowspan="1" colspan="1">频数</th>
            <th rowspan="1" colspan="1">百分比</th>
            <th rowspan="1" colspan="1">累积百分比</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="i in item.options" :key="i.option">
            <td>{{ i.optionDesc }}</td>
            <td>{{ i.frequency }}</td>
            <td>{{ (i.ratio * 100).toFixed(2) + '%' }}</td>
            <td>{{ (i.accumulate * 100).toFixed(2) + '%' }}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="chart-item">
      <div class="title">
        {{ item.name }}
        <div class="tools"></div>
      </div>
      <div class="chart">
        <PieChart :chartOption="item.option" />
        <!-- <LineChart :chartOption="item.option" /> -->
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import LineChart from '../LineChart.vue'
import PieChart from '../PieChart.vue'
import { chartOption } from '../chart'
const prop = defineProps({
  data: {
    type: String,
    default: ''
  }
})
const chartData = ref([])
onMounted(() => {
  const data = { ...prop.data }
  data.items.forEach((item) => {
    item.options.forEach((i, index) => {
      if (index == 0) {
        i.accumulate = i.ratio
      } else {
        i.accumulate = i.ratio + item.options[index - 1].accumulate
      }
    })
    item.option = chartOption(
      item.options.map((i) => {
        return {
          name: i.optionDesc,
          value: i.frequency
        }
      }),
      item.name,
      'basePie'
    )
  })
  console.log(data)

  chartData.value = [...data.items]
})
const handleStackLine = (index) => {
  chartData.value[index].option = chartOption(
    chartData.value[index].data,
    chartData.value[index].chartTitle,
    'stackLine'
  )
}
const handleBaseLine = (index) => {
  chartData.value[index].option = chartOption(
    chartData.value[index].data,
    chartData.value[index].chartTitle,
    'baseLine'
  )
}
</script>

<style lang="scss" scoped>
.chart-item {
  // border-bottom: 1px solid #ccc;
  padding-bottom: 10px;
  margin: 20px 0 40px 0;
  box-shadow: 0 0 10px #ccc;
  padding: 10px;
  border-radius: 5px;
}
.title {
  display: flex;
  justify-content: space-between;
  position: relative;
  padding: 20px;
  font-weight: 700;
  font-size: 20px;
  line-height: 30px;
  .tools {
    display: flex;
  }
  &::after {
    position: absolute;
    content: '';
    left: 0;
    top: 25px;
    height: 20px;
    width: 5px;
    background-color: #178aec;
  }
}
.chart {
  width: 100%;
  height: 400px;
}
.deep-analysis {
  padding: 10px 0;
}
table {
  /* width: 100%; */
  // border-bottom: 1px solid #000;
  // border-top: 1px solid #000;
  /* width: 1137px; */
  border-collapse: collapse;
  font-size: 14px;
  background-color: #fff;
  margin-bottom: 10px;
}

table tr {
  width: 100%;
  /* line-height: 35px; */
  height: 27px;
  /* height: 35px; */
  text-align: center;
}

table tr th {
  /* background: #fafafa; */
  color: #000;
  font-weight: 400;
  /* border: 1px solid #DDDDDD; */
  line-height: 30px;
  text-align: center;
  border-bottom: 1px solid;
}

table tr td {
  color: #000;
  /* border: 1px solid #DDDDDD; */
  /* border-bottom: 2px solid; */
}
</style>
